package org.sport.dbImplement;

import org.sport.dbInterface.ActLinkUserOperation;
import org.sport.model.entityModel.ActivitytableEntity;
import org.sport.model.entityModel.ActlinuserEntity;
import org.sport.model.entityModel.UsertableEntity;
import org.sport.repository.ActLinUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Allen on 2017/9/28.
 */
@Service
public class ActLinUOpeImp implements ActLinkUserOperation {

    @Autowired
    ActLinUserRepository    actLinUserRepository;

    @Autowired
    UserOpeImp              userOpeImp;

    @Autowired
    ActOpeImp               actOpeImp;
    /**
     * 增加一个User进入ACT
     *
     * @param alu
     */
    @Override
    public boolean addOneData(ActlinuserEntity alu) {
        actLinUserRepository.saveAndFlush(alu);
        return true;
    }

    /**
     * 删除一个User从ACT
     *
     * @param alu
     */
    @Override
    public boolean delOneLine(ActlinuserEntity alu) {
        actLinUserRepository.delete(alu);
        return true;
    }

    /**
     * 查找一个Act的所有User
     *
     * @param act
     */
    @Override
    public List<UsertableEntity> findOneGroupAllUsers(ActivitytableEntity act) {
        List<UsertableEntity> userList = new ArrayList<UsertableEntity>();
        List<ActlinuserEntity> actList = actLinUserRepository.findUserByAId(act);
        for (int i = 0;i < actList.size();i++)
        {
            userList.add(userOpeImp.findUserById(actList.get(i).getUsertableByUserId().getId()));
        }
        return userList;
    }

    /**
     * 查找一个User的所有Act
     *
     * @param user
     */
    @Override
    public List<ActivitytableEntity> findOneUserAllAct(UsertableEntity user) {
        List<ActivitytableEntity> actlist = new ArrayList<ActivitytableEntity>();
        List<ActlinuserEntity> alinkList = actLinUserRepository.findActByUserId(user);
        for (int i = 0;i < alinkList.size();i++)
        {
            actlist.add(actOpeImp.findByActId(alinkList.get(i).getActivitytableByActId()));
        }
        return actlist;
    }


}
